package com.customer.controller.coupon;


import com.alibaba.fastjson.JSON;
import com.aop.SysLog;
import com.customer.model.TblCoupon;
import com.customer.model.TblCouponSendInfo;
import com.customer.service.coupon.CouponService;
import com.customer.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    private CouponService couponService;

    // 跳转页面
    @RequestMapping("/couponPage")
    public String couponPage() {
        return "/coupon/couponInfo.html";
    }


    @RequestMapping("/couponSendPage")
    public String couponSendPage() {
        return "/coupon/couponSendInfo.html";
    }

    // 先编写后端，进行接口测试，测试通过根据api中的响应结果进行 设计前端字段
    @ResponseBody
    @RequestMapping("/showCoupon")
    @SysLog(operation = "查询全部优惠券", method = "coupon_showCoupon", module = "后端管理")
    public String showCoupon(int page, int limit, @RequestParam(required = true, defaultValue = "") String couponName) {
        // 创建一个分页对象 ( 当前页数、总页数、总记录数、每页条数、每页数据 )
        ResponseData responseData = couponService.findAllByPage(page, limit, couponName);
        System.out.println(responseData);
        return JSON.toJSONString(responseData);
    }

    @ResponseBody
    @RequestMapping("/addCoupon")
    @SysLog(operation = "添加优惠券", method = "coupon_addCoupon", module = "后端管理")
    // springMVC 自动注入参数( 1.简单数据类型 2.Map 3.对象 4.集合(放在对象中) 5.ModelAndView  )  新增核心就是一个表
    public String addCoupon(TblCoupon tblCoupon, HttpServletRequest request) {
        // 设置创建者的名称

//        Object adminName = request.getSession().getAttribute("adminName");
//        tblCoupon.setCreateUser(adminName.toString());
        tblCoupon.setCreateUser("zs");
        ResponseData responseData = couponService.addCoupon(tblCoupon);
        return JSON.toJSONString(responseData);
    }

    // 优惠券发送列表展示
    @ResponseBody
    @RequestMapping("/showCouponSend")
    @SysLog(operation = "查询全部优惠券发放", method = "coupon_showCouponSend", module = "后端管理")
    public String showCouponSend(int page, int limit) {
        // 创建一个分页对象 ( 当前页数、总页数、总记录数、每页条数、每页数据 )
        ResponseData responseData = couponService.findAllSendByPage(page, limit);
        System.out.println(responseData);
        return JSON.toJSONString(responseData);
    }

    // 发放优惠券
    // 设计到包含 1.普通的字段上传 2.图片文件的上传 fileupload  3.excel表的上传 poi
    @ResponseBody
    @RequestMapping(value = "/addCouponSend", method = RequestMethod.POST)
    @SysLog(operation = "发放优惠券", method = "coupon_addCouponSend", module = "后端管理")
    public String addCouponSend(@RequestParam(value = "multipartFile") MultipartFile multipartFile,
                                TblCouponSendInfo tblCouponSendInfo, HttpServletRequest request) {
        // 从session域中获取当前登录的用户
        Object adminName = request.getSession().getAttribute("adminName");
        tblCouponSendInfo.setOperatorUser(adminName + "");

        // controller层 只做 请求 响应
        ResponseData responseData = couponService.add(multipartFile, tblCouponSendInfo);
        return JSON.toJSONString(responseData);
    }
}
